<script type='text/javascript' src='validation.js'></script>
<script type='text/javascript' src='overlib.js'></script>
<script type='text/javascript' src='overlib_hideform.js'></script>

<div id='overDiv' style='position:absolute; visibility:hidden; z-index:1000; background-color: none;'></div>
<script language='JavaScript'>
<!--
var imgDir = 'images/';
var preloadImages = new Array();
preloadImages[0] = new Image();
preloadImages[0].src = imgDir + 'spacer.gif';
preloadImages[1] = new Image();
preloadImages[1].src = imgDir + 'callout_a_1.gif';
preloadImages[2] = new Image();
preloadImages[2].src = imgDir + 'callout_a_2.gif';
preloadImages[3] = new Image();
preloadImages[3].src = imgDir + 'callout_a_3.gif';
preloadImages[4] = new Image();
preloadImages[4].src = imgDir + 'callout_a_4.gif';
//-->
</script>
<?
$dberror=translate('dberror');
$plan_created=translate('plancreated');
$no_plan_name=translate('noplanname');
if (!isset($form2_sent) && !isset($form_sent)) {
	$params[sid] = $sid;
	$params[action] = "showCreatePlanConditions";
	$url = "$baseURL/?".encode_params($params);
	unset($params);
	$message1=translate('message1',array('url' => "$url"));
} elseif (isset($form_sent) && !isset($form2_sent)) {
	$message1=translate('step_2_title');
} elseif (isset($form2_sent) && !isset($form_sent)) {
	$message1=translate('step_3_title');
}
$message2=translate('message2');
$message3=translate('message3');
$message4=translate('message4');
$message5=translate('message5');
$planname=translate('planname');
$planperiod=translate('planperiod');
$months=translate('months');
$tlservice=translate('tlservice');
$tlquantity=translate('tlquantity');
$tlmo=translate('tlmo');
$tlbaseunit=translate('tlbaseunit');
$tlunitprice=translate('tlunitprice');
$totmonthprice=translate('totmonthprice');
$clear_form=translate('clear_form');
$save_plan=translate('save_plan');
$plan_name_taken=translate('plan_name_taken');
$img_txt_1 = translate('img_txt_1');
$img_txt_2 = translate('img_txt_2');
$img_txt_3 = translate('img_txt_3');
$minprice_th = translate('minprice_th');
$maxprice_th = translate('maxprice_th');
$wholesale_th = translate('wholesale_th');
$price_th = translate('price_th');
$price_th_y = translate('price_th_y');

include('wallet_db.html');

//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='currency, store_id';
	$sql['table']='stores';
	$sql['where']="reseller_id='$id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
//	exit("<br><div class=errormsg>$dberror 2</div><br>");
         $errorMsg = translate('int_error');
         include_once('errorMsg.html');
         exit;

}
$res_currency=$result[0][currency];
$store_id=$result[0][store_id];
#$res_currency='YEN';


//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']="key_name, quantity, $res_currency as currency";
	$sql['table']='plan_elements AS a, plan_prices AS b';
	$sql['where']="a.id=b.id and a.key_name != 'cgi'";
	$sql['options']='order by b.id,quantity';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
//	exit("<br><div class=errormsg>$dberror 3</div><br>");
         $errorMsg = translate('int_error');
         include_once('errorMsg.html');
         exit;

}

if (isset($_POST[form_sent]) && $_POST[form_sent]=='yes') {

	if (empty($_POST[plan_name])) {
//		echo "<br><div class=errormsg>$no_plan_name</div><br>";
	    $errorMsg = translate('noplanname');
        include_once('errorMsg.html');
	} elseif (!preg_match("/^[a-zA-Z0-9\-_]{1,10}$/", $_POST['plan_name']) or in_array($_POST['plan_name'],array('Business','Corporate'))) {
		$errorMsg = translate('planname_doesnt_match');
        include_once('errorMsg.html');
	} else {
		$plan_name=htmlentities($_POST[plan_name]);
		$plan_name=pg_escape_string($_POST[plan_name]);
		//------------------------------------------------------
	        $sql['type']='SELECT';
	        $sql['params']="count(product_id) AS count";
	        $sql['table']='products';
	        $sql['where']="reseller_id='$id' and type='plan' and name='$plan_name'";
	        $sql['options']='';
	        $sql['return']='struct';
		//------------------------------------------------------
		if (!$asdfg=sql_exec($sql,$dbh81,'assoc')) {
//		        exit("<br><div class=errormsg>$dberror 4</div><br>");
	         $errorMsg = translate('int_error');
        	 include_once('errorMsg.html');
        	 exit;

		}
		$plcount=$asdfg[0][count];
		if ($plcount!=0) {
//			echo "<br><div class=errormsg>$plan_name_taken</div><br>";
                        $errorMsg = translate('plan_name_taken');
                        include_once('errorMsg.html');

		} else {
			$period=$_POST['period'];
			foreach ($_POST as $key => $value) {
				if ($key!='plan_name' && $key!='period' && $key!='submit' && $key!='form_sent') {
					if (preg_match("/^key_.*/",$key)) {
						$key=preg_replace("/^(key_)/",'',$key);
						$newarr[$key]=$value;
					}
				}
			}
			foreach ($newarr as $key => $value) {
				if ($key=='disk_space') $value=$value*1024;
				if ($key=='traffic') $value=$value*1024000;
				$services[$key]=$value;
			}
			$tmp=$services[mailbox]*2;
			$services[aliases]=$tmp;
			$services[fpe]=9999;
			$services[ppa]=9999;
			#$services[site_studio]=0;
			$services[ssl]=9999;
			$services[filemanager]=1;
			$services[stats]=1;
			$services[redirect_url]=10;
			$services[backup]='-1';
			$services[webmail]=1;
			$services[phpmy_admin]=1;
			#$services[support]=1;
			$services[template]='-1';
			$services[smtp]=1;
			$services[register_domain]=1;
			#$tmp=$services[domain]*2;
			#$services[subdomain]=$tmp;
			$tmp=$services[sql]*30;
			$services[sql_quota]=$tmp;
			foreach ($result as $rslt) {
				if ($newarr[$rslt[key_name]]==$rslt[quantity])
					$prices[$rslt[key_name]]=$rslt[currency];
			}
			$monpr=array_sum($prices);
			$minpr=$monpr + ($monpr*5)/100;
			$maxpr=sprintf("%01.2f",$monpr + ($monpr*125/100));

			$min_price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$max_price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$wholesale=array(1=>'',3=>'',6=>'',9=>'',12=>'');

			$minpr = sprintf("%01.2f",$minpr);
			$maxpr = sprintf("%01.2f",$maxpr);
			$monpr = sprintf("%01.2f",$monpr);
			$min_price[$period][$res_currency]=$minpr*$period;
			$max_price[$period][$res_currency]=$maxpr*$period;
			$wholesale[$period][$res_currency]=$monpr*$period;
			$price[$period][$res_currency]=$minpr*$period;

			$min_price_array = $min_price;
			$min_price=base64_encode(serialize($min_price));
			$max_price_array = $max_price;
			$max_price=base64_encode(serialize($max_price));
			$price_array = $price;
			$price=base64_encode(serialize($price));
			$wholesale_array = $wholesale;
			$wholesale=base64_encode(serialize($wholesale));
			$services=base64_encode(serialize($services));


			//------------------------------------------------------
                        $sql['type']='SELECT';
                        $sql['params']='max(seq) as max';
                        $sql['table']='products';
                        $sql['where']="reseller_id='$id' AND type='plan'";
                        $sql['options']='';
                        $sql['return']='struct';
            //------------------------------------------------------
            if (!$result=sql_exec($sql,$dbh81,'assoc')) {
//                                exit("<br><div class=errormsg>$dberror 5</div><br>");
		         $errorMsg = translate('int_error');
		         include_once('errorMsg.html');
		         exit();
            }
			$max_seq=$result[0]['max'];
			$max_seq++;

			pg_query($dbh81,'BEGIN');
			$sql="INSERT INTO products (name,store_id,reseller_id,type,
				services,min_price,price,max_price,wholesale,seq,offer_product)
			VALUES('$plan_name','$store_id','$id','plan','$services',
				'$min_price','$price','$max_price','$wholesale','$max_seq','f')";
			if (!$rsut=pg_query($dbh81,$sql)) {
				pg_query($dbh81,'ROLLBACK');
//				exit("<br><div class=errormsg>$dberror 6</div><br>");
			         $errorMsg = translate('int_error');
			         include_once('errorMsg.html');
			         exit;

			}
                        unset($sql);
                        $sql = "SELECT * FROM products WHERE reseller_id = '$id' AND type = 'domain_plan_res'";
                        $res = exec_query($sql,$dbh81);
                        $numres = sql_num_rows($res);
                        unset($sql);
                        if ($numres == 0) {
                            //------------------------------------------------------
                            $sql['type']='SELECT';
                            $sql['params']='name,type,services,min_price,price,max_price,wholesale';
                            $sql['table']='rp_products';
                            $sql['where']="type = 'domain_plan_res'";
                            $sql['options']='';
                            $sql['return']='struct';
                            //------------------------------------------------------
                            if (!$result=sql_exec($sql,$dbh81,'assoc')) {
//                                    exit("<br><div class=errormsg>$dberror</div><br>");
			         $errorMsg = translate('int_error');
			         include_once('errorMsg.html');
			         exit;

                            }
                            unset($sql);
                            $res_name = $result[0]['name'];
                            $res_type = $result[0]['type'];
                            $res_services = base64_encode(serialize($result[0]['services']));
                            $res_min_price = base64_encode(serialize($result[0]['min_price']));
                            $res_price = base64_encode(serialize($result[0]['price']));
                            $res_max_price = base64_encode(serialize($result[0]['max_price']));
                            $res_wholesale = base64_encode(serialize($result[0]['wholesale']));
                            $sql = "INSERT INTO products
                                (name,store_id,reseller_id,type,services,min_price,
                                    price,max_price,wholesale,state)
                                VALUES
                                    ('$res_name','$store_id','$id','$res_type','$res_services',
                                    '$res_min_price','$res_price','$res_max_price',
                                    '$res_wholesale','A')";

                            if (!$rsut=pg_query($dbh81,$sql)) {
                                pg_query($dbh81,'ROLLBACK');
//                                exit("<br><div class=errormsg>$dberror</div><br>");
			         $errorMsg = translate('int_error');
			         include_once('errorMsg.html');
			         exit;

                            }
                            unset($sql);
                            $sql = "SELECT product_id
                                FROM products
                                WHERE reseller_id = '$id'
                                    AND type = 'domain_plan_res'";
                            $res = pg_query($dbh81,$sql);
                            $prod_id = pg_fetch_result($res,0,'product_id');
                            unset($sql,$res);
                            $sql = "SELECT products
                                FROM stores
                                WHERE reseller_id = '$id'
                                    AND store_id = '$store_id'";
                            $res = pg_query($dbh81,$sql);
                            $prods = unserialize(base64_decode(pg_fetch_result($res,0,'products')));
                            $tmp_arr = array_keys($prods['domains_offering']);
                            if (!in_array($prod_id,$tmp_arr)) {
                                $prods['domains_offering'][$prod_id] = 'on';
                            }
                            $prods = base64_encode(serialize($prods));
                            unset($sql,$res);
                            $sql = "UPDATE stores
                                SET products = '$prods'
                                WHERE reseller_id = '$id'
                                AND store_id = '$store_id'";
                            $res = pg_query($dbh81,$sql);
                        }

			unset($sql,$res);
			$sql = "SELECT product_id FROM products WHERE reseller_id='$id' AND name='$plan_name'";
			$res = pg_query($dbh81,$sql);
			$new_plan_id = pg_fetch_result($res,0,'product_id');
			unset($sql,$res);
			$sql = "SELECT products
				FROM stores
				WHERE reseller_id = '$id'
				AND store_id = '$store_id'";
			$res = pg_query($dbh81,$sql);
                        $prods = unserialize(base64_decode(pg_fetch_result($res,0,'products')));
			$prods['plans_offering'][$new_plan_id] = 'on';
			$prods['periods'][$new_plan_id][$period] = 'on';
                        $prods = base64_encode(serialize($prods));
                        unset($sql,$res);
                        $sql = "UPDATE stores
                                SET products = '$prods'
                                WHERE reseller_id = '$id'
                                AND store_id = '$store_id'";
                        $res = pg_query($dbh81,$sql);
                        unset($query);
                        pg_query($dbh81,'COMMIT');
            $params[sid] = $sid;
			$params[action] = "managePlans";
			$manageeurl = "$baseURL/?".encode_params($params);
			$step_2_message_3 = translate('step_2_message_3',array('plan' => $plan_name, 'reseller' => $userName, 'url' => $manageeurl));
			$step_2_message_2 = translate('step_2_message_2',array('plan_name' => "$plan_name"));
			$step_2_message_1 = translate('step_2_message_1');
                        echo "<div align='left'><p>$step_2_message_1</p>";
			echo "<div align=center>
			<table border='0' cellspacing='0' cellpadding='5'>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td valign='middle' align='center'>
					<img src='images/ico_step_2.gif' border='0'>
				</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td valign='middle' align='center'>
					<img src='images/ico_completed_step.gif' border='0'>
				</td>
				<td valign='middle' align='center'>$img_txt_1</td>
				<td valign='middle' align='center'>
					<img src='images/ico_current_step.gif' border='0'>
				</td>
				<td valign='middle' align='center'><font color='#FF6000'>$img_txt_2</font></td>
				<td valign='middle' align='center'><img src='images/ico_next_step.gif' border='0'></td>
				<td valign='middle' align='center'>$img_txt_3</td>
			</tr>
			</table>
			</div><br><br>
			$step_2_message_2";

				$params[sid] = $sid;
				$params[sub] = 'step2';
				$params[action] = "createPlan";
				$url = "$baseURL/?".encode_params($params);
				echo "<script language='JavaScript'>
				<!--
				function calc_price() {
					var mopr = document.getElementById('new_price').value;
					mopr=parseFloat(mopr);
					if (isNaN(mopr)) return;
					document.getElementById('yprice_div').innerHTML = '$' + display_dec_string(mopr*12);
				}

				function display_dec_string(input) {
					var ost = 0;
					var cch = 0;
					var result;
					cch = Math.floor(input);
					if( cch == 0 ) {
						ost = input;
					} else {
						ost = input % cch;
					}
					ost = Math.round(ost * 100);
					if ( ost < 10 ) ost = \"0\" + ost;
					result = cch + \".\" + ost;
					return result;

				}

				function pafY() {
					var minpr = document.getElementById('minpr').value;
					var maxpr = document.getElementById('maxpr').value;
					var new_price = document.getElementById('new_price').value;

					minpr = parseFloat(minpr);
					maxpr = parseFloat(maxpr);
					new_price = parseFloat(new_price);
					if (isNaN(new_price)) {
					alert('Please, type a valid price');
					document.form1.reset();
					return false;
				}
					if (new_price<minpr) {
						alert('Your price must not be below our minimal price of $' + minpr);
						document.form1.reset();
						calc_price();
						return false;
					}
					if (new_price>maxpr) {
						alert('Your price must not be above our maximal price of $' + maxpr);
						document.form1.reset();
						calc_price();
						return false;
					} else {
						return true;
					}
				}
				//-->
				</script><br><br><div align='center'>
				<table class='bTable' cellspacing=0>
				<form action='$url' name='form1' method='POST' onSubmit='return pafY();'>
				<tr>
					<th>$planname</th>
					<th>$minprice_th</th>
					<th>$maxprice_th</th>
					<th>$wholesale_th</th>
					<th>$price_th</th>
					<th>$price_th_y</th>
				</tr>
				<tr>
					<td>$plan_name</td>
					<td align='right'>
						<input type='hidden' id='minpr' name='minpr' value='$minpr'>$".$minpr."</td>
					<td align='right'>
						<input type='hidden' id='maxpr' name='maxpr' value='$maxpr'>$".$maxpr."</td>
					<td align='right'>$".$monpr."</td>
					<td align='right' nowrap>$
						<input type='text' id='new_price' name='new_price' value='$minpr' size=5  onKeyUp='ctrlPriceInput(this);calc_price();'  onBlur='ctrlPriceInput(this);calc_price();'></td>
					<td align='right' nowrap><div align='right' id='yprice_div'>1.00</div></td>
				</tr>
				<tr>
					<td colspan=6 align=center><input type='button' name='clear_button' value='$clear_form' onClick='this.form.reset();calc_price();'>
					<input type='submit' name='submit' value='$save_plan'>
					<input type='hidden' name='plan_name' value='$plan_name'>
					<input type='hidden' name='form2_sent' value='yes'></td>
				</tr>
				</form>
				</table></div>
				<script language='JavaScript'>
				<!--
				calc_price();
				//-->
				</script>";
				echo "<br><br>$step_2_message_3</div>";
				unset($params,$url);
			unset($sql);
		}
	}
} elseif (isset($_POST['form2_sent']) && $_POST['form2_sent'] == 'yes') {
	if (empty($_POST['plan_name'])) {
//		echo "<br><div class=errormsg>No plan name!</div><br>";
                   $errorMsg = translate('noplanname');
                   include_once('errorMsg.html');

	} elseif (empty($_POST['new_price'])) {
		echo "<br><div class=errormsg>No new_price!</div><br>";
	} else {
		$_POST['new_price'] = preg_replace("/,/",'.',$_POST['new_price']);
	$_POST['new_price'] = (real) $_POST['new_price'];
	$_POST['plan_name'] = addslashes($_POST['plan_name']);
	//------------------------------------------------------
	        $sql['type']='SELECT';
	        $sql['params']='product_id, name, min_price, price, max_price, wholesale';
	        $sql['table']='products';
	        $sql['where']="reseller_id='$id' AND type='plan' and name='$_POST[plan_name]'";
	        $sql['options']='';
	        $sql['return']='struct';
	        //------------------------------------------------------
	        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
//	         exit("<br><div class=errormsg>$dberror 5</div><br>");
	         $errorMsg = translate('int_error');
        	 include_once('errorMsg.html');
	         exit;
	        }

		$prod_name = $result[0]['name'];
		$prod_id = $result[0]['product_id'];
		$min_price = $result[0]['min_price'];
		$max_price = $result[0]['max_price'];
		$price = $result[0]['price'];
		$wholesale = $result[0]['wholesale'][12]['USD'];
		$wspmo = $wholesale/12;
		$wspmo = sprintf("%01.2f",$wspmo);
		foreach ($min_price as $value) {
			if (!empty($value)) {
				$mipr = $value['USD'];
			}
		}
		unset($value);
		foreach ($max_price as $value) {
			if (!empty($value)) {
				$mapr = $value['USD'];
			}
		}
		$price = sprintf("%01.3f",$_POST['new_price']);
		$mipr = sprintf("%01.3f",$mipr/12);
		$mapr = sprintf("%01.3f",$mapr/12);
		if ($price < $mipr) {
//			echo "<br><div class=errormsg>Your price must not be below our minimal price</div><br>";
                   $errorMsg = translate('invalid_plan_min_price');
                   include_once('errorMsg.html');
		} elseif ($price > $mapr) {
//			echo "<br><div class=errormsg>Your price must not be above our maximal price</div><br>";
                   $errorMsg = translate('invalid_plan_max_price');
                   include_once('errorMsg.html');
		} else {
			$nprice=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$price = $price*12;
			$price = sprintf("%01.2f",$price);
			$nprice[12]['USD'] = $price;
			$nprice = base64_encode(serialize($nprice));
			$query = "UPDATE products SET price = '$nprice' WHERE product_id = '$prod_id' AND reseller_id = '$id'";
			if (!$result = exec_query($query,$dbh81)) {
//				echo "<br><div class=errormsg>$dberror</div><br>";
	        	 $errorMsg = translate('int_error');
        		 include_once('errorMsg.html');
		         exit();
			} else {
				echo "<script language='JavaScript'>
				<!--
				var myWin = '';


				function openWin(url, name, properties) {
				    if (myWin && myWin.open && !myWin.closed) {
					myWin.close();
				    }
				    myWin = window.open(url, name, properties);
				    return false;
				}
				//-->
				</script>";
				$params[sid] = $sid;
				$params[action] = "showPlanDetails";
				$params[rid] = $id;
				$params[pid] = $prod_id;
				$detailsurl = "$baseURL/?".encode_params($params);

				unset($params);
				$params[sid] = $sid;
				$params[action] = "moveClient2Plan";
				$moveurl = "$baseURL/?".encode_params($params);

				$step_3_message_1=translate('step_3_message_1');
				$step_3_message_2=translate('step_3_message_2',array('plan_name' => "$prod_name",'url' => "$detailsurl"));
				$step_3_message_3=translate('step_3_message_3',array('plan_name' => "$prod_name",'wsp_mo' => "$wspmo",'url' => "$detailsurl"));
				$step_3_message_4=translate('step_3_message_4',array('plan_name' => "$prod_name", 'url' => "$moveurl"));
				$signup_now=translate('signup_now');
				echo "<div align='left'><p>$step_3_message_1</p>";
				echo "<div align=center>
				<table border='0' cellspacing='0' cellpadding='5'>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td valign='middle' align='center'>
						<img src='images/ico_step_3.gif' border='0'>
					</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td valign='middle' align='center'>
						<img src='images/ico_completed_step.gif' border='0'>
					</td>
					<td valign='middle' align='center'>$img_txt_1</td>
					<td valign='middle' align='center'>
						<img src='images/ico_completed_step.gif' border='0'>
					</td>
					<td valign='middle' align='center'>$img_txt_2</td>
					<td valign='middle' align='center'>
						<img src='images/ico_current_step.gif' border='0'>
					</td>
					<td valign='middle' align='center'><font color='#FF6000'>$img_txt_3</font></td>
				</tr>
				</table>
				</div><br><br>
				$step_3_message_2<br><br>$step_3_message_3<br>";
				$params[sid] = $sid;
			        $params[action] = "order";
				$params[plan] = $prod_id;
			        $orderurl = "$baseURL/?".encode_params($params);
				echo "<br><input type=button name=order_me value='$signup_now' onClick=\"self.location='$orderurl'\";><br><br>";
				echo $step_3_message_4."</div>";

			}
		}
	}


} else {
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='count(product_id) as count';
		$sql['table']='products';
		$sql['where']="reseller_id='$id' and type='plan'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$rt=sql_exec($sql,$dbh81,'assoc')) {
	//       exit("<br><div class=errormsg>$dberror 1</div><br>");
		 $errorMsg = translate('int_error');
		 include_once('errorMsg.html');
		 exit;

	}
	$count=$rt[0][count];
	if ($count>=20) {
		$params[sid] = $sid;
		$params[action] = "managePlans";
		$manageurl = "$baseURL/?".encode_params($params);
		unset($params);
		$nomoreplans=translate('nomoreplans',array('count' => "$count",'url' => "$manageurl"));
	//	exit("<br><div align='left'>$nomoreplans</div><br>");
		$errorMsg = translate('nomoreplans',array('count' => $count,'url' => $manageurl));
		include_once('errorMsg.html');

	} else {
		foreach ($result as $rt) {
			$jsarr[]='key_'.$rt[key_name];
		}
		$jsarr=array_flip($jsarr);
		$jsarr=array_flip($jsarr);

		if (preg_match("/usd/i",$res_currency)) {
			$res_curr='$';
		} elseif (preg_match("/yen/i",$res_currency)) {
			$res_curr='&yen';
		}

		echo "<script language='JavaScript'>\n
		<!--\n";
		foreach ($jsarr as $gt) {
		echo "var ".$gt."_prices = new Array();\n";
		echo $gt."_prices['0']=0;\n";
		}
		#foreach ($jsarr as $gt) {
		#	$jsarr2[]="key_".$gt;
		#}
		$jsstr=implode("\",\"",$jsarr);
		echo "\n
		var names_selects = new Array (\"$jsstr\");";
		echo "\n";
		foreach ($result as $rt) {
			echo 'key_'.$rt[key_name]."_prices"."['".$rt[quantity]."']='".$rt[currency]."';\n";
		}
		echo "\n";
		echo "
		function display_dec_string(input) {
			var ost = 0;
			var cch = 0;
			var result;
			cch = Math.floor(input);
			if( cch == 0 ) {
				ost = input;
			} else {
				ost = input % cch;
			}
			ost = Math.round(ost * 100);
			if ( ost < 10 ) ost = \"0\" + ost;
			result = cch + \".\" + ost;
			return result;

		}

		function calc_me (obj) {
			var total = 0;
			for (i=0; i<names_selects.length; i++) {
				var el = eval(\"obj.form.\" +names_selects[i]);
				var val = el.options[el.selectedIndex].value;
				get_price = eval(el.name + \"_prices['\" + val + \"']\");
				if (el.name == 'key_disk_space') {
					val = val/50;
				}
				get_unit_price = (val != 0) ? get_price/val : 0;
				document.getElementById(el.name + '_row').innerHTML = \"$res_curr\" +display_dec_string(get_price);
				document.getElementById(el.name + '_unit').innerHTML = \"$res_curr\" +display_dec_string(get_unit_price);
				tmp_v = el.options[el.selectedIndex].value;
				tmp_p = eval(names_selects[i] + \"_prices['\" + tmp_v + \"']\");
				if (el && el.options[el.selectedIndex].value) total+= parseFloat(tmp_p);
			}
			document.getElementById('total').innerHTML = \"$res_curr\" +display_dec_string(total);
		}\n
		";


		echo "var myWin = '';


		function openWin(url, name, properties) {
		    if (myWin && myWin.open && !myWin.closed) {
			myWin.close();
		    }
		    myWin = window.open(url, name, properties);
		    return false;
		}
		function handleSupportSelect (obj) {
              if (obj.name == 'key_support') {
                   if (obj.options[obj.selectedIndex].value == '9999') {
                        if (!confirm('Are you sure you want to provide your own support?')) obj.options[0].selected = true;
                   }
              }
              calc_me(obj);
         }
		//-->
		</script>\n";

		echo "<div align=left>
		<p>$message1</p>";
		if ($res_currency=='USD') {
			echo "<p>$message2</p></div>";
		} else {
			echo "<p>$message5</p></div>";
		}
		#echo "<div align=left><p>You have $count plans on store!</p></div>";

		echo "<div align=center>
		<table border='0' cellspacing='0' cellpadding='5'>
		<tr>
			<td valign='middle' align='center'>
				<img src='images/ico_step_1.gif' border='0'>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td valign='middle' align='center'>
				<img src='images/ico_current_step.gif' border='0'>
			</td>
			<td valign='middle' align='center'><font color='#FF6000'>$img_txt_1</font></td>
			<td valign='middle' align='center'>
				<img src='images/ico_next_step.gif' border='0'>
			</td>
			<td valign='middle' align='center'>$img_txt_2</td>
			<td valign='middle' align='center'><img src='images/ico_next_step.gif' border='0'></td>
			<td valign='middle' align='center'>$img_txt_3</td>
		</tr>
		</table>
		</div><br>";

		$params[sid] = $sid;
		$params[action] = "createPlan";
		$url = "$baseURL/?".encode_params($params);

			echo "<form name=create_plan action='$url' method='POST'>
		<TABLE class='bTable' cellspacing=0>
			<tr>
				<td>$planname :</td>
				<td><input type=text name=plan_name size=15 maxlength=10></td>
				<td>$planperiod :</td>
				<td><select name=period>
					<option value=12>12 $months</option>
				</select></td>
			</tr>
		</table><br><br>";

		echo "<TABLE class='bTable' cellspacing=0>
			<tr>
				<th>$tlservice</th>
				<th>$tlquantity</th>
				<th nowrap>$tlmo</th>
				<td>&nbsp;</td>
				<th nowrap>$tlbaseunit</th>
				<th nowrap>$tlunitprice</th>
			</tr>";

		//------------------------------------------------------
			$sql['type']='SELECT';
			$sql['params']='*';
			$sql['table']='plan_elements';
			$sql['where']="key_name != 'cgi'";
			$sql['options']='order by seq';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	//		exit("<br><div class=errormsg>$dberror 7</div><br>");
		 $errorMsg = translate('int_error');
		 include_once('errorMsg.html');
		 exit;

		}
		foreach ($result as $res) {

			//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']="quantity, $res_currency AS price";
				$sql['table']='plan_prices';
				$sql['where']="id='$res[id]'";
				$sql['options']='order by quantity';
				$sql['return']='struct';
			//------------------------------------------------------
			if (!$rlt=sql_exec($sql,$dbh81,'assoc')) {
	//			exit("<br><div class=errormsg>$dberror 8</div><br>");
			 $errorMsg = translate('int_error');
			 include_once('errorMsg.html');
			 exit;

			}
			$service=translate("$res[key_name]");
			$tmp = $res['key_name'].'_tool';
			$tooltip = preg_replace("/\n/",'',translate("$tmp"));
			echo "<tr>
				<td><a href='#' onmouseover='overlib(\"".$tooltip."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'>$service</a></td>
				<td><select name='key_".$res[key_name]."' onChange=\"handleSupportSelect(this)\">";
			if ($res['id'] != 1 && $res['id'] != 2 && $res['id'] != 3 && $res['id'] != 4 && $res['id'] != 13 && $res['id'] != 10 && $res['id'] != 14) echo "<option value=0>----</option>\n";
			foreach ($rlt as $vl) {
				if ($res[key_name]=='disk_space') {
					$unit='50 megabytes';
					$unit1='MB';
				} elseif ($res[key_name]=='traffic') {
					$unit='1 gigabyte';
					$unit1='GB';
				} elseif ($res[key_name]=='domain') {
					if ($vl[quantity]==1) {
						$unit1='domain';
					} else {
						$unit1='domains';
					}
					$unit='domain';
				} elseif ($res[key_name]=='mailbox') {
					$unit='mail address';
					$unit1='addresses';
				} elseif ($res[key_name]=='sql') {
					$unit='database';
					if ($vl[quantity]==1) {
						$unit1='database';
					} else {
						$unit1='databases';
					}
				} elseif ($res[key_name]=='ip') {
					$unit='IP address';
					if ($vl[quantity]==1) {
						$unit1='address';
					} else {
						$unit1='addresses';
					}
				} elseif ($res[key_name]=='cgi' or  $res[key_name]=='site_studio') {
					$unit='installation';
					$unit1='unit';
				} elseif ($res[key_name]=='mail_list') {
					$unit='1 list';
					if ($vl[quantity]==1) {
						$unit1='list';
					} else {
						$unit1='lists';
					}
				} elseif ($res[key_name]=='ftp') {
					$unit='account';
					if ($vl[quantity]==1) {
						$unit1='account';
					} else {
						$unit1='accounts';
					}
				} elseif ($res[key_name]=='ssh') {
					$unit='shell';
				} elseif ($res[key_name]=='crontab') {
					$unit='cronjob';
					if ($vl['quantity']==1) {
						$unit1 = 'cronjob';
					} else {
						$unit1 = 'cronjobs';
					}
				} elseif ($res[key_name]=='subdomain') {
					$unit='subdomain';
					if ($vl['quantity']==1) {
						$unit1 = 'subdomain';
					} else {
						$unit1 = 'subdomains';
					}
				} else {
					$unit='1 unit';
					$unit1='unit';
				}

				$vl['price'] = sprintf("%01.2f",$vl['price']);
				if (count($rlt)==1 && $res['key_name'] != 'support') {
					echo "<option value='$vl[quantity]'>Activate - ".$res_curr."$vl[price]</option>\n";
				} elseif (count($rlt)!=1 && $res['key_name'] == 'support') {
					if ($vl['quantity'] == 1) {
						echo "<option value='$vl[quantity]'>Activate - ".$res_curr."$vl[price]</option>\n";
					} else {
						echo "<option value='$vl[quantity]'>Own Customer Support- ".$res_curr."$vl[price]</option>\n";
					}
				} else {
					echo "<option value='$vl[quantity]'>$vl[quantity] $unit1 - ".$res_curr."$vl[price]</option>\n";
				}
			}
			echo "</select></td>
				<td nowrap><div id='key_".$res[key_name]."_row' align=right>".$res_curr."0.00</div></td>
				<td nowrap>&nbsp;</td>
				<td nowrap>$unit</td>
				<td nowrap><div id='key_".$res[key_name]."_unit' align=right>".$res_curr."0.00</div></td>
			</tr>";
		}
		echo "<tr>
			<th colspan=2 align=right>$totmonthprice:</th>
			<th nowrap><div id=total align=right>".$res_curr."0.00</div></th>
			<td>&nbsp;</td>
			<th colspan=2 align=right>&nbsp;</th>
		</tr></table><br><br>";
		echo " <TABLE class='bTable' cellspacing=0>
			<tr>
				<td><input type=reset name=reset_form value='$clear_form' onClick=\"this.form.reset();calc_me(this)\">&nbsp;
					<input type=submit name=submit value='$save_plan'></td>
			</tr>
		</table>
		<input type=hidden name=form_sent value='yes'>
		</form>\n
		<script language='JavaScript'>
			calc_me(document.create_plan.reset_form);\n
		</script>";
		echo "<br><br><div align=left>$message3</div>";
	}
    }
?>

